//
//  M1QuestViewController.m
//  NewMarryU
//
//  Created by apple on 2018/8/1.
//  Copyright © 2018年 Cedar. All rights reserved.
//

#import "M1QuestViewController.h"

@interface M1QuestViewController ()<UITextViewDelegate>
@property (nonatomic ,strong) UITextView *m1_TV1;
@property (nonatomic ,strong) UITextView *m1_TV2;
@property (nonatomic ,strong) UIScrollView *m1_scroll;
@property (nonatomic ,strong) UIButton *m1_addBtn;
@property (nonatomic ,strong) NSMutableArray *m1_images;
@property (nonatomic ,strong) UILabel *m1_place1;
@property (nonatomic ,strong) UILabel *m1_place2;
@end

@implementation M1QuestViewController
-(void)m1_addClick{
    
    
    M1SheetView *m1_sheet = [[M1SheetView alloc]initWithTitles:@[@"拍照",@"从相册选择"]];
    [m1_sheet m1_show];
    
    WEAKSELF;
    m1_sheet.click = ^(NSInteger index) {
        [[M1ImagePick manager] m1_pickImages:^(NSArray *photos) {
            [weakSelf.m1_images addObjectsFromArray:photos];
            [weakSelf m1_loadImages];
        } type:index maxCount:3-weakSelf.m1_images.count vc:weakSelf];
    };
}

-(void)btn_CLICK{
    if (_m1_TV1.text.length == 0) {
        [M1Alert m1_alertWithTitle:nil message:@"请填写您的情感问题"];
        return;
    }
    
    if (_m1_TV2.text.length == 0) {
        [M1Alert m1_alertWithTitle:nil message:@"请填写您的详细描述"];
        return;
    }
    WEAKSELF;
    [M1HttpNet requestWithMethod:@"app/user/user_seek_help" params:@{@"title":_m1_TV1.text,@"content":_m1_TV2.text} success:^(id resDic, NSInteger code, NSString *message) {
        [M1Alert m1_alertWithTitle:nil message:@"您的情感问题已经提交，敬请等待回复" buttonTitles:@[@"确定"] action:^(NSInteger index) {
            [weakSelf.navigationController popViewControllerAnimated:YES];
        }];
    } fail:^(id resDic, NSInteger code, NSString *message) {
        [M1Alert m1_alertWithTitle:nil message:message];
    } showAni:YES];
}
-(void)textViewDidChange:(UITextView *)textView{
    if (textView.text.length > 0) {
        if (textView.tag == 0) {
            [_m1_place1 removeFromSuperview];
        }else{
            [_m1_place2 removeFromSuperview];
        }
    }
}
-(void)m1_tap1{
    [_m1_TV1 becomeFirstResponder];
    [_m1_place1 removeFromSuperview];
}
-(void)m1_tap2{
    [_m1_TV2 becomeFirstResponder];
    [_m1_place2 removeFromSuperview];
}
-(void)m1_loadImages{
    
    for (UIView *view in _m1_scroll.subviews) {
        [view removeFromSuperview];
        
    }
    
    for (int i = 0; i<_m1_images.count; i++) {
        UIImageView *img = [[UIImageView alloc]init];
        img.clipsToBounds = YES;
        img.contentMode = UIViewContentModeScaleAspectFill;
        img.image = _m1_images[i];
        img.frame = CGRectMake(70*i, 0, 65, 65);
        [_m1_scroll addSubview:img];
    }
    
    if (_m1_images.count <3) {
        [_m1_scroll addSubview:_m1_addBtn];
        _m1_addBtn.frame = CGRectMake(70*_m1_images.count, 0, 65, 65);
    }
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.navigationItem.title = @"问题求助";
    _m1_images = [NSMutableArray arrayWithCapacity:0];
    
    UIScrollView *m1_scroll = [[UIScrollView alloc]init];
    [self.view addSubview:m1_scroll];
    [m1_scroll mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_offset(UIEdgeInsetsMake(m1_NAV_HEIGHT, 0, 0, 0));
    }];
    
    UILabel *m1_LB1 = [[UILabel alloc]init];
    m1_LB1.text= @"主要问题";
    m1_LB1.font = m1_font(15);
    [m1_scroll addSubview:m1_LB1];
    [m1_LB1 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(m1_scroll.mas_top).offset(10);
        make.left.equalTo(self.view).offset(20);
    }];
    
    
    UIView *m1_view1 = [[UIView alloc]init];
    m1_view1.backgroundColor = [UIColor whiteColor];
    [m1_scroll addSubview:m1_view1];
    
    [m1_view1 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self.view);
        make.left.equalTo(self.view);
        make.top.equalTo(m1_LB1.mas_bottom).offset(10);
        make.height.mas_offset(100);
    }];
    
    _m1_TV1 = [[UITextView alloc]init];
    _m1_TV1.font = m1_font(15);
    _m1_TV1.delegate = self;
    _m1_TV1.tag = 0;
    [m1_view1 addSubview:_m1_TV1];
    
    [_m1_TV1 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_offset(UIEdgeInsetsMake(5, 5, 5, 5));
    }];
    
    _m1_place1 = [[UILabel alloc]init];
    _m1_place1.text= @"一句话概括你的情感问题(20字以内)";
    _m1_place1.textColor = [UIColor m1_colorHex:0x999999];
    _m1_place1.font = m1_font(15);
    _m1_place1.userInteractionEnabled = YES;
    [_m1_TV1 addSubview:_m1_place1];
    [_m1_place1 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.offset(5);
        make.left.offset(0);
    }];
    
    UITapGestureRecognizer *m1_tap1 = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(m1_tap1)];
    [_m1_place1 addGestureRecognizer:m1_tap1];
    
    UILabel *m1_LB2 = [[UILabel alloc]init];
    m1_LB2.text= @"详细描述";
    m1_LB2.font = m1_font(15);
    [m1_scroll addSubview:m1_LB2];
    [m1_LB2 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(m1_view1.mas_bottom).offset(10);
        make.left.equalTo(self.view.mas_left).offset(20);
    }];
    
    
    UIView *m1_view2 = [[UIView alloc]init];
    m1_view2.backgroundColor = [UIColor whiteColor];
    [m1_scroll addSubview:m1_view2];
    
    [m1_view2 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.view.mas_left);
        make.top.equalTo(m1_LB2.mas_bottom).offset(10);
        make.height.mas_offset(130);
        make.right.equalTo(self.view);
    }];
    
    _m1_TV2 = [[UITextView alloc]init];
    _m1_TV2.font = m1_font(15);
    _m1_TV2.tag = 1;
    _m1_TV2.delegate = self;
    [m1_view2 addSubview:_m1_TV2];
    
    [_m1_TV2 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_offset(UIEdgeInsetsMake(5, 5, 5, 5));
    }];
    
    _m1_place2 = [[UILabel alloc]init];
    _m1_place2.text= @"请详细描述你的问题，提供更多的背景介绍";
    _m1_place2.textColor = [UIColor m1_colorHex:0x999999];
    _m1_place2.font = m1_font(15);
    [_m1_TV2 addSubview:_m1_place2];
    [_m1_place2 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.offset(5);
        make.left.offset(0);
    }];
    UITapGestureRecognizer *m1_tap2 = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(m1_tap2)];
    [_m1_place2 addGestureRecognizer:m1_tap2];
    
    UILabel *m1_LB3 = [[UILabel alloc]init];
    m1_LB3.text= @"照片上传";
    m1_LB3.font = m1_font(15);
    [m1_scroll addSubview:m1_LB3];
    [m1_LB3 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(m1_view2.mas_bottom).offset(10);
        make.left.equalTo(self.view.mas_left).offset(20);
    }];
    
    _m1_scroll = [[UIScrollView alloc]init];
    [m1_scroll addSubview:_m1_scroll];
    
    [_m1_scroll mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.view.mas_left).offset(20);
        make.right.equalTo(self.view.mas_right).offset(-20);
        make.top.equalTo(m1_LB3.mas_bottom).offset(10);
        make.height.mas_offset(65);
    }];
    
    _m1_addBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [_m1_addBtn setBackgroundImage:[UIImage imageNamed:@"pyq_add"] forState:0];
    [_m1_scroll addSubview:_m1_addBtn];
    _m1_addBtn.frame = CGRectMake(0, 0, 65, 65);
    [_m1_addBtn addTarget:self action:@selector(m1_addClick) forControlEvents:UIControlEventTouchUpInside];
    
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    [btn setBackgroundImage:[UIImage imageNamed:@"btn_color"] forState:0];
    [btn setTitle:@"提交" forState:0];
    btn.layer.cornerRadius = 25;
    btn.clipsToBounds = YES;
    [btn addTarget:self action:@selector(btn_CLICK) forControlEvents:UIControlEventTouchUpInside];
    [btn setTitleColor:[UIColor whiteColor] forState:0];
    btn.titleLabel.font = m1_font(16);
    [m1_scroll addSubview:btn];
    [btn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.view);
        make.top.equalTo(_m1_scroll.mas_bottom).offset(15);
        make.size.mas_offset(CGSizeMake(170, 50));
        make.bottom.lessThanOrEqualTo(m1_scroll.mas_bottom).offset(-34);
    }];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
